package org.black.phoneUtility.sms.reply.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.black.phoneUtility.database.sqlite.PhoneUtilitySQLiteOpenHelper;
import org.black.phoneUtility.sms.reply.app.service.SmsReplyNotificationIntentService;
import org.black.phoneUtility.sms.reply.app.service.SmsReplyScheduleNotifyNotificationInentService;
import org.black.phoneUtility.sms.reply.appwidget.SmsReplyAppWidgetProvider;
import org.black.phoneUtility.sms.reply.database.dataModel.NoneReplyNumber;
import org.black.phoneUtility.sms.reply.database.dataModel.NoneReplyNumberFromContact;
import org.black.phoneUtility.sms.reply.database.dataModel.NoneReplyNumberFromContacts;
import org.black.phoneUtility.sms.reply.database.dataModel.NoneReplyNumberFromMatchSmsWithKeyword;
import org.black.phoneUtility.sms.reply.database.dataModel.NoneReplyNumberFromNumbersFromContactGroup;
import org.black.phoneUtility.sms.reply.database.dataModel.NoneReplyNumberFromUserInput;
import org.black.phoneUtility.sms.reply.database.dataModel.ReplyTimesMode;
import org.black.phoneUtility.sms.reply.database.dataModel.ReplyTimesModeAlways;
import org.black.phoneUtility.sms.reply.database.dataModel.ReplyTimesModeLimitedTimes;
import org.black.phoneUtility.sms.reply.database.dataModel.ReplyToSpecificNumber;
import org.black.phoneUtility.sms.reply.database.dataModel.ReplyToSpecificNumberFromContact;
import org.black.phoneUtility.sms.reply.database.dataModel.ReplyToSpecificNumberFromNumbersFromContactGroup;
import org.black.phoneUtility.sms.reply.database.dataModel.ReplyToSpecificNumberFromUserInput;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFrom;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromAllNumbers;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromAllNumbersInMyContacts;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromAllNumbersNotInMyContacts;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromChooseFromMyContacts;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromMatchSmsWithKeyword;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromNumbersFromContactGroup;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromUserInput;
import org.black.phoneUtility.sms.reply.database.dataModel.SmsReply;
import org.black.phoneUtility.sms.reply.provider.SmsReplyContract;
import org.black.phoneUtility.sms.reply.service.exception.NoPhoneMessageException;
import org.black.phoneUtility.sms.reply.service.exception.NoReplyMessageException;
import org.black.phoneUtility.sms.reply.service.exception.NoSmsMessageException;
import org.black.phoneUtility.sms.reply.service.exception.ReplyDateTimeExpiredException;
import org.black.phoneUtility.sms.reply.service.exception.ReplyDateTimeInvalidException;
import org.black.phoneUtility.sms.reply.service.exception.SmsReplyNotExistException;
import org.black.phoneUtility.sms.reply.util.DateTimeUtil;

import android.appwidget.AppWidgetManager;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

public class SmsReplyService {
	private static final Integer TRUE_VALUE = 1;
	private static final Integer FALSE_VALUE = 0;
	private static final String LOG_TAG = SmsReplyService.class.getName();

	private Context context = null;

	public SmsReplyService(Context context) {
		this.context = context;
	}

	public Uri insertOrUpdate(SmsReply smsReply) throws RemoteException,
			OperationApplicationException {
		if (smsReply == null) {
			throw new IllegalArgumentException("smsReply is null!");
		}
		ArrayList<ContentProviderOperation> contentProviderOperations = new ArrayList<ContentProviderOperation>();

		boolean isInsert = true;
		if (smsReply.getId() != null) {
			isInsert = false;
		}
		int insertSmsReplyIndex = 0;
		{
			ContentValues smsReplyContentValues = new ContentValues();
			smsReplyContentValues.put(SmsReplyContract.SmsReplies.TITLE,
					smsReply.getTitle());
			smsReplyContentValues
					.put(SmsReplyContract.SmsReplies.SHOULD_RESPONSE_FROM_PHONE,
							smsReply.getShouldResponseToMissedCall() != null
									&& smsReply.getShouldResponseToMissedCall() == true ? 1
									: 0);
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.SHOULD_RESPONSE_FROM_SMS,
					smsReply.getShouldResponseToSms() != null
							&& smsReply.getShouldResponseToSms() == true ? 1
							: 0);
			smsReplyContentValues
					.put(SmsReplyContract.SmsReplies.PHONE_SMS_USE_DIFFERENT_MESSAGE,
							smsReply.getMissedCallAndSmsUsedDifferentMessages() != null
									&& smsReply
											.getMissedCallAndSmsUsedDifferentMessages() == true ? 1
									: 0);
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.PHONE_MESSAGE,
					smsReply.getPhoneMessage());
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.SMS_MESSAGE,
					smsReply.getSmsMessage() == null ? "" : smsReply
							.getSmsMessage());
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.IS_ACTIVATED,
					smsReply.getIsActivated() != null
							&& smsReply.getIsActivated() == true ? 1 : 0);
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.ACTIVATION_DATE_TIME,
					smsReply.getActivationDateTime() == null ? new Date()
							.getTime() : smsReply.getActivationDateTime()
							.getTime());
			smsReplyContentValues
					.put(SmsReplyContract.SmsReplies.REPLY_MODE,
							smsReply.getReplyMode() == null ? SmsReplyContract.SmsReplies.REPLY_MODE_VALUE_REPLY_DIRECTLY
									: smsReply.getReplyMode());
			if (isInsert == true) {
				smsReplyContentValues.put(
						SmsReplyContract.SmsReplies.CREATEION_DATE_TIME,
						smsReply.getCreationDateTime() == null ? System
								.currentTimeMillis() : smsReply
								.getCreationDateTime().getTime());
			}
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.MODIFICATION_DATE_TIME,
					smsReply.getModificationDateTime() == null ? System
							.currentTimeMillis() : smsReply
							.getModificationDateTime().getTime());
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.ENABLE_AVOID_EXCESSIVE_REPLY,
					smsReply.isEnableAvoidExcessiveReply() == true ? 1 : 0);
			smsReplyContentValues
					.put(SmsReplyContract.SmsReplies.ENABLE_NOT_RESPONSE_TO_READ_MESSAGE,
							true == smsReply.isEnableNotResponseToReadMessage() ? 1
									: 0);
			if (smsReply.getDelayTimeForNotResponseToReadMessage() == null) {
				smsReplyContentValues
						.putNull(SmsReplyContract.SmsReplies.DELAY_TIME_FOR_NOT_RESPONSE_TO_READ_MESSAGE);
			} else {
				smsReplyContentValues
						.put(SmsReplyContract.SmsReplies.DELAY_TIME_FOR_NOT_RESPONSE_TO_READ_MESSAGE,
								smsReply.getDelayTimeForNotResponseToReadMessage());
			}
			smsReplyContentValues
					.put(SmsReplyContract.SmsReplies.ENABLE_NOT_RESPONSE_TO_RESPONSE_CALL,
							true == smsReply
									.isEnableNotResponseToRespondedMissedCall() ? 1
									: 0);
			if (smsReply.getDelayTimeForNotResponseToRespondedMissedCall() == null) {
				smsReplyContentValues
						.putNull(SmsReplyContract.SmsReplies.DELAY_TIME_FOR_NOT_RESPONSE_TO_RESPONSE_CALL);
			} else {
				smsReplyContentValues
						.put(SmsReplyContract.SmsReplies.DELAY_TIME_FOR_NOT_RESPONSE_TO_RESPONSE_CALL,
								smsReply.getDelayTimeForNotResponseToRespondedMissedCall());
			}
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.NOTIFY_AFTER_SENDING_MESSAGE,
					smsReply.isNotifyAfterSendingMessage() == true ? 1 : 0);

			// Reply Date Time Mode Setting
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.REPLY_DATE_TIME_TYPE,
					smsReply.getReplyDateTimeModeType());
			if (smsReply.getExpiredAfterMilliSeconds() == null) {
				smsReplyContentValues
						.putNull(SmsReplyContract.SmsReplies.EXPIRATED_AFTER_MILLISECONDS);
			} else {
				smsReplyContentValues
						.put(SmsReplyContract.SmsReplies.EXPIRATED_AFTER_MILLISECONDS,
								smsReply.getExpiredAfterMilliSeconds());
			}
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.ENABLE_RINGER_MODE_SILENT,
					smsReply.isEnableRingerModeSilent() == true ? 1 : 0);

			Calendar periodBeginCalendar = Calendar.getInstance();
			if (smsReply.getPeriodStartDateTime() != null) {
				periodBeginCalendar.setTime(smsReply.getPeriodStartDateTime());
			}
			periodBeginCalendar.set(Calendar.MILLISECOND, 0);
			periodBeginCalendar.set(Calendar.SECOND, 0);
			Calendar periodEndCalendar = Calendar.getInstance();
			if (smsReply.getPeriodEndDateTime() != null) {
				periodEndCalendar.setTime(smsReply.getPeriodEndDateTime());
			}
			periodEndCalendar.set(Calendar.MILLISECOND, 0);
			periodEndCalendar.set(Calendar.SECOND, 0);
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.PERIOD_START_DATE_TIME,
					periodBeginCalendar.getTimeInMillis());
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.PERIOD_END_DATE_TIME,
					periodEndCalendar.getTimeInMillis());

			Calendar repeatBeginCalendar = Calendar.getInstance();
			if (smsReply.getRepeatStartTime() != null) {
				repeatBeginCalendar.setTime(smsReply.getRepeatStartTime());
			}
			repeatBeginCalendar.set(Calendar.MILLISECOND, 0);
			Calendar repeatEndCalendar = Calendar.getInstance();
			if (smsReply.getRepeatEndTime() != null) {
				repeatEndCalendar.setTime(smsReply.getRepeatEndTime());
			}
			repeatEndCalendar.set(Calendar.MILLISECOND, 0);
			repeatEndCalendar.set(Calendar.SECOND, 0);
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.REPEAT_START_TIME,
					repeatBeginCalendar.getTimeInMillis());
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.REPEAT_END_TIME,
					repeatEndCalendar.getTimeInMillis());
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.IS_SUNDAY_ENABLE,
					smsReply.isSundayEnable() == true ? 1 : 0);
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.IS_MONDAY_ENABLE,
					smsReply.isMondayEnable() == true ? 1 : 0);
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.IS_TUESDAY_ENABLE,
					smsReply.isTuesdayEnable() == true ? 1 : 0);
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.IS_WEDNESDAY_ENABLE,
					smsReply.isWednesdayEnable() == true ? 1 : 0);
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.IS_THURSDAY_ENABLE,
					smsReply.isThursdayEnable() == true ? 1 : 0);
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.IS_FRIDAY_ENABLE,
					smsReply.isFridayEnable() == true ? 1 : 0);
			smsReplyContentValues.put(
					SmsReplyContract.SmsReplies.IS_SATURDAY_ENABLE,
					smsReply.isSaturdayEnable() == true ? 1 : 0);
			if (isInsert == true) {
				insertSmsReplyIndex = contentProviderOperations.size();
				contentProviderOperations.add(ContentProviderOperation
						.newInsert(SmsReplyContract.SmsReplies.CONTENT_URI)
						.withValues(smsReplyContentValues).build());
			} else {
				contentProviderOperations.add(ContentProviderOperation
						.newUpdate(SmsReplyContract.SmsReplies.CONTENT_URI)
						.withSelection(
								SmsReplyContract.SmsReplies._ID + " = ? ",
								new String[] { smsReply.getId().toString() })
						.withValues(smsReplyContentValues).build());
			}

		}
		if (isInsert == false) {
			ContentValues updateActivaionRecord = new ContentValues();
			updateActivaionRecord.put(
					SmsReplyContract.ActivationRecords.IS_ACTIVATED,
					FALSE_VALUE.intValue());
			contentProviderOperations
					.add(ContentProviderOperation
							.newUpdate(
									SmsReplyContract.ActivationRecords.CONTENT_URI)
							.withSelection(
									SmsReplyContract.ActivationRecords.REFERENCE_SMS_REPLY_ID
											+ " = ? ",
									new String[] { smsReply.getId().toString() })
							.withValues(updateActivaionRecord).build());
		}
		{
			if (isInsert == false) {
				if (smsReply.getResponseNumberFroms() == null
						|| smsReply.getResponseNumberFroms().size() == 0) {
					contentProviderOperations
							.add(ContentProviderOperation
									.newDelete(
											SmsReplyContract.ResponseNumberFrom.CONTENT_URI)
									.withSelection(
											SmsReplyContract.ResponseNumberFrom.REFERENCE_SMS_REPLY_ID
													+ " = ? ",
											new String[] { smsReply.getId()
													.toString() }).build());
				} else {
					List<String> responseNumberFromsId = new ArrayList<String>();
					for (ResponseNumberFrom responseNumberFrom : smsReply
							.getResponseNumberFroms()) {
						if (responseNumberFrom.getId() != null
								&& responseNumberFrom.getId() != 0) {
							responseNumberFromsId.add(responseNumberFrom
									.getId().toString());
						}
					}
					if (responseNumberFromsId.size() > 0) {
						List<String> whereArgus = new ArrayList<String>();
						whereArgus.add(smsReply.getId().toString());
						whereArgus.addAll(responseNumberFromsId);
						contentProviderOperations
								.add(ContentProviderOperation
										.newDelete(
												SmsReplyContract.ResponseNumberFrom.CONTENT_URI)
										.withSelection(
												SmsReplyContract.ResponseNumberFrom.REFERENCE_SMS_REPLY_ID
														+ " = ? "
														+ " AND "
														+ SmsReplyContract.ResponseNumberFrom._ID
														+ " NOT IN ("
														+ generatePlaceHolder(responseNumberFromsId
																.size()) + ")",
												whereArgus
														.toArray(new String[whereArgus
																.size()]))
										.build());
					} else {
						contentProviderOperations
								.add(ContentProviderOperation
										.newDelete(
												SmsReplyContract.ResponseNumberFrom.CONTENT_URI)
										.withSelection(
												SmsReplyContract.ResponseNumberFrom.REFERENCE_SMS_REPLY_ID
														+ " = ? ",
												new String[] { smsReply.getId()
														.toString() }).build());
					}
				}
			}
			if (smsReply.getResponseNumberFroms() != null) {
				List<String> responseNumberFromIds = new ArrayList<String>();
				for (ResponseNumberFrom responseNumberFrom : smsReply
						.getResponseNumberFroms()) {
					if (responseNumberFrom.getId() != null
							&& responseNumberFrom.getId() != 0) {
						responseNumberFromIds.add(responseNumberFrom.getId()
								.toString());
					}
				}
				for (ResponseNumberFrom responseNumberFrom : smsReply
						.getResponseNumberFroms()) {
					ContentValues responseNumbeFromContentValues = new ContentValues();
					responseNumbeFromContentValues.put(
							SmsReplyContract.ResponseNumberFrom.TYPE,
							responseNumberFrom.getType());
					if (responseNumberFrom instanceof ResponseNumberFromAllNumbers) {
					} else if (responseNumberFrom instanceof ResponseNumberFromAllNumbersInMyContacts) {
					} else if (responseNumberFrom instanceof ResponseNumberFromChooseFromMyContacts) {
						ResponseNumberFromChooseFromMyContacts responseNumberFromContact = (ResponseNumberFromChooseFromMyContacts) responseNumberFrom;

						if (responseNumberFromContact.getName() == null) {
							responseNumbeFromContentValues
									.putNull(SmsReplyContract.ResponseNumberFrom.NAME);
						} else {
							responseNumbeFromContentValues.put(
									SmsReplyContract.ResponseNumberFrom.NAME,
									responseNumberFromContact.getName());
						}
						responseNumbeFromContentValues
								.put(SmsReplyContract.ResponseNumberFrom.PHONE_NUMBER,
										responseNumberFromContact
												.getPhoneNumber());
					} else if (responseNumberFrom instanceof ResponseNumberFromUserInput) {
						ResponseNumberFromUserInput responseNumberFromUserInput = (ResponseNumberFromUserInput) responseNumberFrom;

						if (responseNumberFromUserInput.getName() == null) {
							responseNumbeFromContentValues
									.putNull(SmsReplyContract.ResponseNumberFrom.NAME);
						} else {
							responseNumbeFromContentValues.put(
									SmsReplyContract.ResponseNumberFrom.NAME,
									responseNumberFromUserInput.getName());
						}
						responseNumbeFromContentValues
								.put(SmsReplyContract.ResponseNumberFrom.PHONE_NUMBER,
										responseNumberFromUserInput
												.getPhoneNumber());
					} else if (responseNumberFrom instanceof ResponseNumberFromMatchSmsWithKeyword) {
						ResponseNumberFromMatchSmsWithKeyword responseNumberFromMatchSmsWithKeyword = (ResponseNumberFromMatchSmsWithKeyword) responseNumberFrom;

						responseNumbeFromContentValues
								.put(SmsReplyContract.ResponseNumberFrom.SMS_KEYWORD,
										responseNumberFromMatchSmsWithKeyword
												.getKeyword());
					} else if (responseNumberFrom instanceof ResponseNumberFromNumbersFromContactGroup) {
						ResponseNumberFromNumbersFromContactGroup responseNumberFromNumbersFromContactGroup = (ResponseNumberFromNumbersFromContactGroup) responseNumberFrom;
						if (responseNumberFromNumbersFromContactGroup.getName() == null) {
							responseNumbeFromContentValues
									.putNull(SmsReplyContract.ResponseNumberFrom.NAME);
						} else {
							responseNumbeFromContentValues.put(
									SmsReplyContract.ResponseNumberFrom.NAME,
									responseNumberFromNumbersFromContactGroup
											.getName());
						}
						if (responseNumberFromNumbersFromContactGroup
								.getGroupName() == null) {
							responseNumbeFromContentValues
									.putNull(SmsReplyContract.ResponseNumberFrom.GROUP_NAME);
						} else {
							responseNumbeFromContentValues
									.put(SmsReplyContract.ResponseNumberFrom.GROUP_NAME,
											responseNumberFromNumbersFromContactGroup
													.getGroupName());
						}
						responseNumbeFromContentValues
								.put(SmsReplyContract.ResponseNumberFrom.PHONE_NUMBER,
										responseNumberFromNumbersFromContactGroup
												.getPhoneNumber());
					}
					ContentProviderOperation.Builder builder = null;
					if (responseNumberFrom.getId() == null) {
						responseNumbeFromContentValues
								.put(SmsReplyContract.ResponseNumberFrom.CREATION_DATE_TIME,
										responseNumberFrom
												.getCreationDateTime() == null ? System
												.currentTimeMillis()
												: responseNumberFrom
														.getCreationDateTime()
														.getTime());
						builder = ContentProviderOperation
								.newInsert(SmsReplyContract.ResponseNumberFrom.CONTENT_URI);
						builder.withValues(responseNumbeFromContentValues);
						if (isInsert == true) {
							builder.withValueBackReference(
									SmsReplyContract.ResponseNumberFrom.REFERENCE_SMS_REPLY_ID,
									insertSmsReplyIndex);
						} else {
							builder.withValue(
									SmsReplyContract.ResponseNumberFrom.REFERENCE_SMS_REPLY_ID,
									smsReply.getId());
						}
					} else {
						builder = ContentProviderOperation
								.newUpdate(
										SmsReplyContract.ResponseNumberFrom.CONTENT_URI)
								.withSelection(
										SmsReplyContract.ResponseNumberFrom._ID
												+ " = ? ",
										new String[] { responseNumberFrom
												.getId().toString() })
								.withValues(responseNumbeFromContentValues);
					}

					contentProviderOperations.add(builder.build());
				}
			}
		}

		{
			if (isInsert == false) {
				if (smsReply.getReplyToSpecificNumbers() == null
						|| smsReply.getReplyToSpecificNumbers().size() == 0) {
					contentProviderOperations
							.add(ContentProviderOperation
									.newDelete(
											SmsReplyContract.ReplyToSpecificNumbers.CONTENT_URI)
									.withSelection(
											SmsReplyContract.ReplyToSpecificNumbers.REFERENCE_SMS_REPLY_ID
													+ " = ? ",
											new String[] { smsReply.getId()
													.toString() }).build());
				} else {
					List<String> replyToSpecificNumberIds = new ArrayList<String>();
					for (ReplyToSpecificNumber replyToSpecificNumber : smsReply
							.getReplyToSpecificNumbers()) {
						if (replyToSpecificNumber.getId() != null
								&& replyToSpecificNumber.getId() != 0) {
							replyToSpecificNumberIds.add(replyToSpecificNumber
									.getId().toString());
						}
					}
					if (replyToSpecificNumberIds.size() > 0) {
						List<String> whereArgus = new ArrayList<String>();
						whereArgus.add(smsReply.getId().toString());
						whereArgus.addAll(replyToSpecificNumberIds);
						contentProviderOperations
								.add(ContentProviderOperation
										.newDelete(
												SmsReplyContract.ReplyToSpecificNumbers.CONTENT_URI)
										.withSelection(
												SmsReplyContract.ReplyToSpecificNumbers.REFERENCE_SMS_REPLY_ID
														+ " = ? "
														+ " AND "
														+ SmsReplyContract.ReplyToSpecificNumbers._ID
														+ " NOT IN ("
														+ generatePlaceHolder(replyToSpecificNumberIds
																.size()) + ")",
												whereArgus
														.toArray(new String[whereArgus
																.size()]))
										.build());
					} else {
						contentProviderOperations
								.add(ContentProviderOperation
										.newDelete(
												SmsReplyContract.ReplyToSpecificNumbers.CONTENT_URI)
										.withSelection(
												SmsReplyContract.ReplyToSpecificNumbers.REFERENCE_SMS_REPLY_ID
														+ " = ? ",
												new String[] { smsReply.getId()
														.toString() }).build());

					}
				}
			}
			if (smsReply.getReplyToSpecificNumbers() != null) {
				for (ReplyToSpecificNumber replyToSpecificNumber : smsReply
						.getReplyToSpecificNumbers()) {
					ContentValues replyToSpecificNumberContentValues = new ContentValues();

					replyToSpecificNumberContentValues.put(
							SmsReplyContract.ReplyToSpecificNumbers.TYPE,
							replyToSpecificNumber.getType());
					if (replyToSpecificNumber instanceof ReplyToSpecificNumberFromUserInput) {
						ReplyToSpecificNumberFromUserInput replyToSpecificNumberFromUserInput = (ReplyToSpecificNumberFromUserInput) replyToSpecificNumber;

						replyToSpecificNumberContentValues
								.put(SmsReplyContract.ReplyToSpecificNumbers.PHONE_NUMBER,
										replyToSpecificNumberFromUserInput
												.getPhoneNumber());
						if (replyToSpecificNumberFromUserInput.getName() == null) {
							replyToSpecificNumberContentValues
									.putNull(SmsReplyContract.ReplyToSpecificNumbers.NAME);
						} else {
							replyToSpecificNumberContentValues
									.put(SmsReplyContract.ReplyToSpecificNumbers.NAME,
											replyToSpecificNumberFromUserInput
													.getName());
						}
					} else if (replyToSpecificNumber instanceof ReplyToSpecificNumberFromContact) {
						ReplyToSpecificNumberFromContact replyToSpecificNumberFromContact = (ReplyToSpecificNumberFromContact) replyToSpecificNumber;

						replyToSpecificNumberContentValues
								.put(SmsReplyContract.ReplyToSpecificNumbers.PHONE_NUMBER,
										replyToSpecificNumberFromContact
												.getPhoneNumber());
						if (replyToSpecificNumberFromContact.getName() == null) {
							replyToSpecificNumberContentValues
									.putNull(SmsReplyContract.ReplyToSpecificNumbers.NAME);
						} else {
							replyToSpecificNumberContentValues
									.put(SmsReplyContract.ReplyToSpecificNumbers.NAME,
											replyToSpecificNumberFromContact
													.getName());
						}
					} else if (replyToSpecificNumber instanceof ReplyToSpecificNumberFromNumbersFromContactGroup) {
						ReplyToSpecificNumberFromNumbersFromContactGroup replyToSpecificNumberFromNumbersFromContactGroup = (ReplyToSpecificNumberFromNumbersFromContactGroup) replyToSpecificNumber;

						replyToSpecificNumberContentValues
								.put(SmsReplyContract.ReplyToSpecificNumbers.PHONE_NUMBER,
										replyToSpecificNumberFromNumbersFromContactGroup
												.getPhoneNumber());
						if (replyToSpecificNumberFromNumbersFromContactGroup
								.getName() == null) {
							replyToSpecificNumberContentValues
									.putNull(SmsReplyContract.ReplyToSpecificNumbers.NAME);
						} else {
							replyToSpecificNumberContentValues
									.put(SmsReplyContract.ReplyToSpecificNumbers.NAME,
											replyToSpecificNumberFromNumbersFromContactGroup
													.getName());
						}
						if (replyToSpecificNumberFromNumbersFromContactGroup
								.getGroupName() == null) {
							replyToSpecificNumberContentValues
									.putNull(SmsReplyContract.ReplyToSpecificNumbers.GROUP_NAME);
						} else {
							replyToSpecificNumberContentValues
									.put(SmsReplyContract.ReplyToSpecificNumbers.GROUP_NAME,
											replyToSpecificNumberFromNumbersFromContactGroup
													.getGroupName());
						}
					}
					ContentProviderOperation.Builder builder = null;
					if (replyToSpecificNumber.getId() == null) {
						replyToSpecificNumberContentValues
								.put(SmsReplyContract.ReplyToSpecificNumbers.CREATION_DATE_TIME,
										replyToSpecificNumber
												.getCreationDateTime() == null ? new Date()
												.getTime()
												: replyToSpecificNumber
														.getCreationDateTime()
														.getTime());
						builder = ContentProviderOperation
								.newInsert(
										SmsReplyContract.ReplyToSpecificNumbers.CONTENT_URI)
								.withValues(replyToSpecificNumberContentValues);
						if (isInsert == true) {
							builder.withValueBackReference(
									SmsReplyContract.ReplyToSpecificNumbers.REFERENCE_SMS_REPLY_ID,
									insertSmsReplyIndex);
						} else {
							builder.withValue(
									SmsReplyContract.ReplyToSpecificNumbers.REFERENCE_SMS_REPLY_ID,
									smsReply.getId()).withValues(
									replyToSpecificNumberContentValues);
						}
					} else {
						builder = ContentProviderOperation
								.newUpdate(
										SmsReplyContract.ReplyToSpecificNumbers.CONTENT_URI)
								.withSelection(
										SmsReplyContract.ReplyToSpecificNumbers._ID
												+ " = ? ",
										new String[] { replyToSpecificNumber
												.getId().toString() })
								.withValues(replyToSpecificNumberContentValues);
					}

					contentProviderOperations.add(builder.build());
				}
			}
		}

		{
			if (isInsert == false) {
				if (smsReply.getNoneReplyNumbers() == null
						|| smsReply.getNoneReplyNumbers().size() == 0) {
					contentProviderOperations
							.add(ContentProviderOperation
									.newDelete(
											SmsReplyContract.NoneReplyNumbers.CONTENT_URI)
									.withSelection(
											SmsReplyContract.NoneReplyNumbers.REFERENCE_SMS_REPLY_ID
													+ " = ? ",
											new String[] { smsReply.getId()
													.toString() }).build());
				} else {
					List<String> noneReplyNumberIds = new ArrayList<String>();
					for (NoneReplyNumber noneReplyNumber : smsReply
							.getNoneReplyNumbers()) {
						if (noneReplyNumber.getId() != null
								&& noneReplyNumber.getId() != 0) {
							noneReplyNumberIds.add(noneReplyNumber.getId()
									.toString());
						}
					}
					if (noneReplyNumberIds.size() > 0) {
						List<String> whereArgus = new ArrayList<String>();
						whereArgus.add(smsReply.getId().toString());
						whereArgus.addAll(noneReplyNumberIds);
						contentProviderOperations
								.add(ContentProviderOperation
										.newDelete(
												SmsReplyContract.NoneReplyNumbers.CONTENT_URI)
										.withSelection(
												SmsReplyContract.NoneReplyNumbers.REFERENCE_SMS_REPLY_ID
														+ " = ? "
														+ " AND "
														+ SmsReplyContract.NoneReplyNumbers._ID
														+ " NOT IN ("
														+ generatePlaceHolder(noneReplyNumberIds
																.size()) + ")",
												whereArgus
														.toArray(new String[whereArgus
																.size()]))
										.build());
					} else {
						contentProviderOperations
								.add(ContentProviderOperation
										.newDelete(
												SmsReplyContract.NoneReplyNumbers.CONTENT_URI)
										.withSelection(
												SmsReplyContract.NoneReplyNumbers.REFERENCE_SMS_REPLY_ID
														+ " = ? ",
												new String[] { smsReply.getId()
														.toString() }).build());

					}
				}
			}

			for (NoneReplyNumber noneReplyNumber : smsReply
					.getNoneReplyNumbers()) {
				ContentValues noneReplyNumberContentValues = new ContentValues();
				noneReplyNumberContentValues.put(
						SmsReplyContract.NoneReplyNumbers.TYPE,
						noneReplyNumber.getType());
				if (noneReplyNumber instanceof NoneReplyNumberFromContacts) {
				} else if (noneReplyNumber instanceof NoneReplyNumberFromUserInput) {
					NoneReplyNumberFromUserInput noneReplyNumberFromUserInput = (NoneReplyNumberFromUserInput) noneReplyNumber;
					noneReplyNumberContentValues.put(
							SmsReplyContract.NoneReplyNumbers.PHONE_NUMBER,
							noneReplyNumberFromUserInput.getPhoneNumber());
					if (noneReplyNumberFromUserInput.getName() == null) {
						noneReplyNumberContentValues
								.putNull(SmsReplyContract.NoneReplyNumbers.NAME);
					} else {
						noneReplyNumberContentValues.put(
								SmsReplyContract.NoneReplyNumbers.NAME,
								noneReplyNumberFromUserInput.getName());
					}
				} else if (noneReplyNumber instanceof NoneReplyNumberFromContact) {
					NoneReplyNumberFromContact noneReplyNumberFromContact = (NoneReplyNumberFromContact) noneReplyNumber;
					noneReplyNumberContentValues.put(
							SmsReplyContract.NoneReplyNumbers.PHONE_NUMBER,
							noneReplyNumberFromContact.getPhoneNumber());
					if (noneReplyNumberFromContact.getName() == null) {
						noneReplyNumberContentValues
								.putNull(SmsReplyContract.NoneReplyNumbers.NAME);
					} else {
						noneReplyNumberContentValues.put(
								SmsReplyContract.NoneReplyNumbers.NAME,
								noneReplyNumberFromContact.getName());
					}
				} else if (noneReplyNumber instanceof NoneReplyNumberFromMatchSmsWithKeyword) {
					NoneReplyNumberFromMatchSmsWithKeyword noneReplyNumberFromMatchSmsWithKeyword = (NoneReplyNumberFromMatchSmsWithKeyword) noneReplyNumber;
					noneReplyNumberContentValues
							.put(SmsReplyContract.NoneReplyNumbers.SMS_KEYWORD,
									noneReplyNumberFromMatchSmsWithKeyword
											.getKeyword());
				} else if (noneReplyNumber instanceof NoneReplyNumberFromNumbersFromContactGroup) {
					NoneReplyNumberFromNumbersFromContactGroup noneReplyNumberFromNumbersFromContactGroup = (NoneReplyNumberFromNumbersFromContactGroup) noneReplyNumber;
					noneReplyNumberContentValues.put(
							SmsReplyContract.NoneReplyNumbers.PHONE_NUMBER,
							noneReplyNumberFromNumbersFromContactGroup
									.getPhoneNumber());
					if (noneReplyNumberFromNumbersFromContactGroup.getName() == null) {
						noneReplyNumberContentValues
								.putNull(SmsReplyContract.NoneReplyNumbers.NAME);
					} else {
						noneReplyNumberContentValues.put(
								SmsReplyContract.NoneReplyNumbers.NAME,
								noneReplyNumberFromNumbersFromContactGroup
										.getName());
					}
					if (noneReplyNumberFromNumbersFromContactGroup
							.getGroupName() == null) {
						noneReplyNumberContentValues
								.putNull(SmsReplyContract.NoneReplyNumbers.GROUP_NAME);
					} else {
						noneReplyNumberContentValues.put(
								SmsReplyContract.NoneReplyNumbers.GROUP_NAME,
								noneReplyNumberFromNumbersFromContactGroup
										.getGroupName());
					}
				}
				ContentProviderOperation.Builder builder = null;
				if (noneReplyNumber.getId() == null) {
					noneReplyNumberContentValues
							.put(SmsReplyContract.NoneReplyNumbers.CREATION_DATE_TIME,
									noneReplyNumber.getCreationDateTime() == null ? new Date()
											.getTime() : noneReplyNumber
											.getCreationDateTime().getTime());
					builder = ContentProviderOperation.newInsert(
							SmsReplyContract.NoneReplyNumbers.CONTENT_URI)
							.withValues(noneReplyNumberContentValues);
					if (isInsert == true) {
						builder.withValueBackReference(
								SmsReplyContract.NoneReplyNumbers.REFERENCE_SMS_REPLY_ID,
								insertSmsReplyIndex);
					} else {
						builder.withValue(
								SmsReplyContract.NoneReplyNumbers.REFERENCE_SMS_REPLY_ID,
								smsReply.getId());
					}
				} else {
					builder = ContentProviderOperation
							.newUpdate(
									SmsReplyContract.NoneReplyNumbers.CONTENT_URI)
							.withSelection(
									SmsReplyContract.NoneReplyNumbers._ID
											+ " = ? ",
									new String[] { noneReplyNumber.getId()
											.toString() })
							.withValues(noneReplyNumberContentValues);
				}

				contentProviderOperations.add(builder.build());
			}

		}

		{
			if (smsReply.getReplyTimesModes() != null) {
				for (ReplyTimesMode replyTimesMode : smsReply
						.getReplyTimesModes()) {
					ContentValues replyTimesModeContentValues = new ContentValues();
					replyTimesModeContentValues
							.put(SmsReplyContract.ReplyTimesModes.IS_ACTIVATED,
									replyTimesMode.getIsActivated() == true
											&& replyTimesMode.getIsActivated() != null ? 1
											: 0);
					replyTimesModeContentValues
							.put(SmsReplyContract.ReplyTimesModes.ACTIVATION_DATE_TIME,
									replyTimesMode.getActivationDateTime() == null ? new Date()
											.getTime() : replyTimesMode
											.getActivationDateTime().getTime());
					replyTimesModeContentValues
							.put(SmsReplyContract.ReplyTimesModes.REFERENCE_SMS_REPLY_ID,
									smsReply.getId());
					replyTimesModeContentValues.put(
							SmsReplyContract.ReplyTimesModes.TYPE,
							replyTimesMode.getType());
					if (replyTimesMode instanceof ReplyTimesModeAlways) {
					} else if (replyTimesMode instanceof ReplyTimesModeLimitedTimes) {
						ReplyTimesModeLimitedTimes replyTimesModeLimitedTimes = (ReplyTimesModeLimitedTimes) replyTimesMode;
						replyTimesModeContentValues
								.put(SmsReplyContract.ReplyTimesModes.TIMES,
										replyTimesModeLimitedTimes.getTimes() == 0 ? 1
												: replyTimesModeLimitedTimes
														.getTimes());
					}

					ContentProviderOperation.Builder builder = null;
					if (replyTimesMode.getId() == null) {
						builder = ContentProviderOperation.newInsert(
								SmsReplyContract.ReplyTimesModes.CONTENT_URI)
								.withValues(replyTimesModeContentValues);
						if (isInsert == true) {
							builder.withValueBackReference(
									SmsReplyContract.ReplyTimesModes.REFERENCE_SMS_REPLY_ID,
									insertSmsReplyIndex);
						} else {
							builder.withValue(
									SmsReplyContract.ReplyTimesModes.REFERENCE_SMS_REPLY_ID,
									smsReply.getId());
						}
					} else {
						builder = ContentProviderOperation
								.newUpdate(
										SmsReplyContract.ReplyTimesModes.CONTENT_URI)
								.withSelection(
										SmsReplyContract.ReplyTimesModes._ID
												+ " = ? ",
										new String[] { replyTimesMode.getId()
												.toString() })
								.withValues(replyTimesModeContentValues);
					}
					contentProviderOperations.add(builder.build());

				}
			}
		}

		ContentProviderResult[] results = this.context.getContentResolver()
				.applyBatch(SmsReplyContract.AUTHORITY,
						contentProviderOperations);

		Uri result = results[insertSmsReplyIndex].uri;
		if (result == null) {
			result = ContentUris.withAppendedId(
					SmsReplyContract.SmsReplies.CONTENT_URI, smsReply.getId());
		}
		if (isInsert == false) {
			List<Integer> appwidgetIds = new ArrayList<Integer>();
			Cursor cursor = null;
			try {
				cursor = this.context
						.getContentResolver()
						.query(SmsReplyContract.SmsReplyAppWidgetIds.CONTENT_URI,
								new String[] { SmsReplyContract.SmsReplyAppWidgetIds.APP_WIDGET_ID },
								SmsReplyContract.SmsReplyAppWidgetIds.REFERENCE_SMS_REPLY_ID
										+ " = ?",
								new String[] { String.valueOf(smsReply.getId()) },
								null, null);
				if (cursor.moveToFirst() == true) {
					do {
						appwidgetIds
								.add(cursor.getInt(cursor
										.getColumnIndex(SmsReplyContract.SmsReplyAppWidgetIds.APP_WIDGET_ID)));
					} while (cursor.moveToNext() == true);
				}
			} catch (Throwable throwable) {
				Log.e(LOG_TAG, "Update SmsReply: " + smsReply.getId()
						+ " to inactivated error!", throwable);
			} finally {
				if (cursor != null) {
					try {
						cursor.close();
					} catch (Throwable th) {
						Log.e(LOG_TAG, "Unable to close cursor.", th);
					}
				}
			}
			AppWidgetManager appWidgetManager = AppWidgetManager
					.getInstance(context);
			for (Integer appId : appwidgetIds) {
				SmsReplyAppWidgetProvider.updateAppWidget(context,
						appWidgetManager, smsReply.getId(), appId,
						smsReply.getIsActivated(), smsReply.getTitle());
			}
			Intent scheduleNotifyNotificationServiceIntent = new Intent(
					context,
					SmsReplyScheduleNotifyNotificationInentService.class);
			Bundle argumentsForSchedule = new Bundle();
			argumentsForSchedule
					.putInt(SmsReplyScheduleNotifyNotificationInentService.ARGUMENTS_ACTION_TYPE,
							SmsReplyScheduleNotifyNotificationInentService.ACTION_TYPE_SCHEDULE_SPECIFIC_SMS_REPLY);
			argumentsForSchedule
					.putLong(
							SmsReplyScheduleNotifyNotificationInentService.ARGUMENTS_SMS_REPLY_ID,
							smsReply.getId());
			scheduleNotifyNotificationServiceIntent
					.putExtras(argumentsForSchedule);
			context.startService(scheduleNotifyNotificationServiceIntent);
			Log.d(LOG_TAG,
					"Start SmsReplyScheduleNotifyNotificationInentService!");
		}
		return result;
	}

	public Uri copy(long smsReplyId) throws RemoteException,
			OperationApplicationException {
		SmsReply smsReply = this.read(smsReplyId);
		if (smsReply != null) {
			Date creationDateTime = new Date();
			smsReply.setId(null);
			smsReply.setIsActivated(false);
			smsReply.setTitle(smsReply.getTitle() + "("
					+ this.context.getString(android.R.string.copy) + ")");
			smsReply.setCreationDateTime(creationDateTime);
			smsReply.setModificationDateTime(creationDateTime);
			if (smsReply.getResponseNumberFroms() != null) {
				for (ResponseNumberFrom responseNumberFrom : smsReply
						.getResponseNumberFroms()) {
					responseNumberFrom.setId(null);
					responseNumberFrom.setCreationDateTime(creationDateTime);
				}
			}
			if (smsReply.getReplyToSpecificNumbers() != null) {
				for (ReplyToSpecificNumber replyToSpecificNumber : smsReply
						.getReplyToSpecificNumbers()) {
					replyToSpecificNumber.setId(null);
					replyToSpecificNumber.setCreationDateTime(creationDateTime);
				}
			}
			if (smsReply.getNoneReplyNumbers() != null) {
				for (NoneReplyNumber noneReplyNumber : smsReply
						.getNoneReplyNumbers()) {
					noneReplyNumber.setId(null);
					noneReplyNumber.setCreationDateTime(creationDateTime);
				}
			}
			if (smsReply.getReplyTimesModes() != null) {
				for (ReplyTimesMode replyTimesMode : smsReply
						.getReplyTimesModes()) {
					replyTimesMode.setId(null);
					replyTimesMode.setActivationDateTime(creationDateTime);
				}
			}
			return this.insertOrUpdate(smsReply);
		}
		return null;
	}

	public SmsReply read(Long id) {
		SmsReply result = null;
		try {

			Cursor smsReplyCursor = null;
			try {
				smsReplyCursor = this.context.getContentResolver().query(
						SmsReplyContract.SmsReplies.CONTENT_URI, null,
						SmsReplyContract.SmsReplies._ID + " = ? ",
						new String[] { id.toString() }, null, null);
				if (smsReplyCursor.moveToFirst() == true) {
					result = new SmsReply();
					result.setId(smsReplyCursor.getLong(smsReplyCursor
							.getColumnIndex(SmsReplyContract.SmsReplies._ID)));

					result.setTitle(smsReplyCursor.getString(smsReplyCursor
							.getColumnIndex(SmsReplyContract.SmsReplies.TITLE)));
					result.setShouldResponseToMissedCall(smsReplyCursor.getInt(smsReplyCursor
							.getColumnIndex(SmsReplyContract.SmsReplies.SHOULD_RESPONSE_FROM_PHONE)) == 1 ? true
							: false);
					result.setShouldResponseToSms(smsReplyCursor.getInt(smsReplyCursor
							.getColumnIndex(SmsReplyContract.SmsReplies.SHOULD_RESPONSE_FROM_SMS)) == 1 ? true
							: false);

					result.setMissedCallAndSmsUsedDifferentMessages(smsReplyCursor.getInt(smsReplyCursor
							.getColumnIndex(SmsReplyContract.SmsReplies.PHONE_SMS_USE_DIFFERENT_MESSAGE)) == 1 ? true
							: false);

					result.setPhoneMessage(smsReplyCursor.getString(smsReplyCursor
							.getColumnIndex(SmsReplyContract.SmsReplies.PHONE_MESSAGE)));

					result.setSmsMessage(smsReplyCursor.getString(smsReplyCursor
							.getColumnIndex(SmsReplyContract.SmsReplies.SMS_MESSAGE)));

					result.setIsActivated(smsReplyCursor.getInt(smsReplyCursor
							.getColumnIndex(SmsReplyContract.SmsReplies.IS_ACTIVATED)) == 1 ? true
							: false);
					Long activationDateTimeMilliSeconds = (smsReplyCursor
							.isNull(smsReplyCursor
									.getColumnIndex(SmsReplyContract.SmsReplies.ACTIVATION_DATE_TIME)) == true ? null
							: smsReplyCursor
									.getLong((smsReplyCursor
											.getColumnIndex(SmsReplyContract.SmsReplies.ACTIVATION_DATE_TIME))));
					result.setActivationDateTime(activationDateTimeMilliSeconds == null ? null
							: new Date(activationDateTimeMilliSeconds
									.longValue()));

					result.setReplyMode(smsReplyCursor.getInt(smsReplyCursor
							.getColumnIndex(SmsReplyContract.SmsReplies.REPLY_MODE)));

					Long creationDateTimeMilliSeconds = (smsReplyCursor
							.isNull(smsReplyCursor
									.getColumnIndex(SmsReplyContract.SmsReplies.CREATEION_DATE_TIME)) == true ? null
							: smsReplyCursor
									.getLong((smsReplyCursor
											.getColumnIndex(SmsReplyContract.SmsReplies.CREATEION_DATE_TIME))));
					result.setActivationDateTime(creationDateTimeMilliSeconds == null ? null
							: new Date(creationDateTimeMilliSeconds.longValue()));

					Long modificationDateTimeMilliSeconds = (smsReplyCursor
							.isNull(smsReplyCursor
									.getColumnIndex(SmsReplyContract.SmsReplies.MODIFICATION_DATE_TIME)) == true ? null
							: smsReplyCursor
									.getLong((smsReplyCursor
											.getColumnIndex(SmsReplyContract.SmsReplies.MODIFICATION_DATE_TIME))));
					result.setActivationDateTime(creationDateTimeMilliSeconds == null ? null
							: new Date(modificationDateTimeMilliSeconds
									.longValue()));

					result.setEnableAvoidExcessiveReply((smsReplyCursor.getInt(smsReplyCursor
							.getColumnIndex(SmsReplyContract.SmsReplies.ENABLE_AVOID_EXCESSIVE_REPLY)) == 1 ? true
							: false));
					result.setEnableNotResponseToReadMessage(smsReplyCursor.getInt(smsReplyCursor
							.getColumnIndex(SmsReplyContract.SmsReplies.ENABLE_NOT_RESPONSE_TO_READ_MESSAGE)) == 1 ? true
							: false);
					if (smsReplyCursor
							.isNull(smsReplyCursor
									.getColumnIndex(SmsReplyContract.SmsReplies.DELAY_TIME_FOR_NOT_RESPONSE_TO_READ_MESSAGE)) == false) {
						result.setDelayTimeForNotResponseToReadMessage(smsReplyCursor.getLong(smsReplyCursor
								.getColumnIndex(SmsReplyContract.SmsReplies.DELAY_TIME_FOR_NOT_RESPONSE_TO_READ_MESSAGE)));
					}
					result.setEnableNotResponseToRespondedMissedCall(smsReplyCursor.getInt(smsReplyCursor
							.getColumnIndex(SmsReplyContract.SmsReplies.ENABLE_NOT_RESPONSE_TO_RESPONSE_CALL)) == 1 ? true
							: false);
					if (smsReplyCursor
							.isNull(smsReplyCursor
									.getColumnIndex(SmsReplyContract.SmsReplies.DELAY_TIME_FOR_NOT_RESPONSE_TO_RESPONSE_CALL)) == false) {
						result.setDelayTimeForNotResponseToRespondedMissedCall(smsReplyCursor.getLong(smsReplyCursor
								.getColumnIndex(SmsReplyContract.SmsReplies.DELAY_TIME_FOR_NOT_RESPONSE_TO_RESPONSE_CALL)));
					}
					result.setEnableRingerModeSilent(smsReplyCursor.getInt(smsReplyCursor
							.getColumnIndexOrThrow(SmsReplyContract.SmsReplies.ENABLE_RINGER_MODE_SILENT)) == 1 ? true
							: false);
					/* Start */
					// TODO: New Column For SMS Reply
					/* End */
					result.setNotifyAfterSendingMessage(smsReplyCursor.getInt(smsReplyCursor
							.getColumnIndex(SmsReplyContract.SmsReplies.NOTIFY_AFTER_SENDING_MESSAGE)) == 1 ? true
							: false);
					if (smsReplyCursor
							.isNull(smsReplyCursor
									.getColumnIndex(SmsReplyContract.SmsReplies.REPLY_DATE_TIME_TYPE)) == false) {
						result.setReplyDateTimeModeType(smsReplyCursor.getInt(smsReplyCursor
								.getColumnIndex(SmsReplyContract.SmsReplies.REPLY_DATE_TIME_TYPE)));
					}
					if (smsReplyCursor
							.isNull(smsReplyCursor
									.getColumnIndex(SmsReplyContract.SmsReplies.EXPIRATED_AFTER_MILLISECONDS)) == false) {
						result.setExpiredAfterMilliSeconds(smsReplyCursor.getLong(smsReplyCursor
								.getColumnIndex(SmsReplyContract.SmsReplies.EXPIRATED_AFTER_MILLISECONDS)));
					}
					if (smsReplyCursor
							.isNull(smsReplyCursor
									.getColumnIndex(SmsReplyContract.SmsReplies.PERIOD_START_DATE_TIME)) == false) {
						result.setPeriodStartDateTime(new Date(
								smsReplyCursor.getLong(smsReplyCursor
										.getColumnIndex(SmsReplyContract.SmsReplies.PERIOD_START_DATE_TIME))));
					}
					if (smsReplyCursor
							.isNull(smsReplyCursor
									.getColumnIndex(SmsReplyContract.SmsReplies.PERIOD_END_DATE_TIME)) == false) {
						result.setPeriodEndDateTime(new Date(
								smsReplyCursor.getLong(smsReplyCursor
										.getColumnIndex(SmsReplyContract.SmsReplies.PERIOD_END_DATE_TIME))));
					}
					if (smsReplyCursor
							.isNull(smsReplyCursor
									.getColumnIndex(SmsReplyContract.SmsReplies.REPEAT_START_TIME)) == false) {
						result.setRepeatStartTime(new Date(
								smsReplyCursor.getLong(smsReplyCursor
										.getColumnIndex(SmsReplyContract.SmsReplies.REPEAT_START_TIME))));
					}
					if (smsReplyCursor
							.isNull(smsReplyCursor
									.getColumnIndex(SmsReplyContract.SmsReplies.REPEAT_END_TIME)) == false) {
						result.setRepeatEndTime(new Date(
								smsReplyCursor.getLong(smsReplyCursor
										.getColumnIndex(SmsReplyContract.SmsReplies.REPEAT_END_TIME))));
					}
					if (smsReplyCursor
							.isNull(smsReplyCursor
									.getColumnIndex(SmsReplyContract.SmsReplies.IS_SUNDAY_ENABLE)) == false) {
						result.setSundayEnable(smsReplyCursor.getInt(smsReplyCursor
								.getColumnIndex(SmsReplyContract.SmsReplies.IS_SUNDAY_ENABLE)) == 1 ? true
								: false);
					}
					if (smsReplyCursor
							.isNull(smsReplyCursor
									.getColumnIndex(SmsReplyContract.SmsReplies.IS_MONDAY_ENABLE)) == false) {
						result.setMondayEnable(smsReplyCursor.getInt(smsReplyCursor
								.getColumnIndex(SmsReplyContract.SmsReplies.IS_MONDAY_ENABLE)) == 1 ? true
								: false);
					}
					if (smsReplyCursor
							.isNull(smsReplyCursor
									.getColumnIndex(SmsReplyContract.SmsReplies.IS_TUESDAY_ENABLE)) == false) {
						result.setTuesdayEnable(smsReplyCursor.getInt(smsReplyCursor
								.getColumnIndex(SmsReplyContract.SmsReplies.IS_TUESDAY_ENABLE)) == 1 ? true
								: false);
					}
					if (smsReplyCursor
							.isNull(smsReplyCursor
									.getColumnIndex(SmsReplyContract.SmsReplies.IS_WEDNESDAY_ENABLE)) == false) {
						result.setWednesdayEnable(smsReplyCursor.getInt(smsReplyCursor
								.getColumnIndex(SmsReplyContract.SmsReplies.IS_WEDNESDAY_ENABLE)) == 1 ? true
								: false);
					}
					if (smsReplyCursor
							.isNull(smsReplyCursor
									.getColumnIndex(SmsReplyContract.SmsReplies.IS_THURSDAY_ENABLE)) == false) {
						result.setThursdayEnable(smsReplyCursor.getInt(smsReplyCursor
								.getColumnIndex(SmsReplyContract.SmsReplies.IS_THURSDAY_ENABLE)) == 1 ? true
								: false);
					}
					if (smsReplyCursor
							.isNull(smsReplyCursor
									.getColumnIndex(SmsReplyContract.SmsReplies.IS_FRIDAY_ENABLE)) == false) {
						result.setFridayEnable(smsReplyCursor.getInt(smsReplyCursor
								.getColumnIndex(SmsReplyContract.SmsReplies.IS_FRIDAY_ENABLE)) == 1 ? true
								: false);
					}
					if (smsReplyCursor
							.isNull(smsReplyCursor
									.getColumnIndex(SmsReplyContract.SmsReplies.IS_SATURDAY_ENABLE)) == false) {
						result.setSaturdayEnable(smsReplyCursor.getInt(smsReplyCursor
								.getColumnIndex(SmsReplyContract.SmsReplies.IS_SATURDAY_ENABLE)) == 1 ? true
								: false);
					}

					{
						Cursor replyTimesModeCursor = null;
						try {
							replyTimesModeCursor = this.context
									.getContentResolver()
									.query(SmsReplyContract.ReplyTimesModes.CONTENT_URI,
											null,
											SmsReplyContract.ReplyTimesModes.REFERENCE_SMS_REPLY_ID
													+ " = ? ",
											new String[] { id.toString() },
											SmsReplyContract.ReplyTimesModes._ID
													+ " ASC");
							List<ReplyTimesMode> replyTimesModes = new ArrayList<ReplyTimesMode>();

							result.setReplyTimesModes(replyTimesModes);
							if (replyTimesModeCursor.moveToFirst() == true) {
								do {
									ReplyTimesMode replyTimesMode = null;
									int type = replyTimesModeCursor
											.getInt(replyTimesModeCursor
													.getColumnIndex(SmsReplyContract.ReplyTimesModes.TYPE));
									if (type == ReplyTimesMode.ALWAYS_TYPE) {
										replyTimesMode = new ReplyTimesModeAlways();
									} else if (type == ReplyTimesMode.LIMITED_TIMES_TYPE) {
										replyTimesMode = new ReplyTimesModeLimitedTimes();
										((ReplyTimesModeLimitedTimes) replyTimesMode)
												.setTimes(replyTimesModeCursor
														.getInt(replyTimesModeCursor
																.getColumnIndex(SmsReplyContract.ReplyTimesModes.TIMES)));
									}
									replyTimesMode
											.setId(replyTimesModeCursor
													.getInt(replyTimesModeCursor
															.getColumnIndex(SmsReplyContract.ReplyTimesModes._ID)));
									replyTimesMode
											.setIsActivated(replyTimesModeCursor
													.getInt(replyTimesModeCursor
															.getColumnIndex(SmsReplyContract.ReplyTimesModes.IS_ACTIVATED)) == 1 ? true
													: false);
									Long replyTimesModeActivationDateTimeMilliSeconds = (replyTimesModeCursor
											.isNull(replyTimesModeCursor
													.getColumnIndex(SmsReplyContract.ReplyTimesModes.ACTIVATION_DATE_TIME)) == true ? null
											: replyTimesModeCursor
													.getLong((replyTimesModeCursor
															.getColumnIndex(SmsReplyContract.ReplyTimesModes.ACTIVATION_DATE_TIME))));
									replyTimesMode
											.setActivationDateTime(replyTimesModeActivationDateTimeMilliSeconds == null ? null
													: new Date(
															replyTimesModeActivationDateTimeMilliSeconds
																	.longValue()));
									replyTimesModes.add(replyTimesMode);
								} while (replyTimesModeCursor.moveToNext() == true);
							}
						} finally {
							if (replyTimesModeCursor != null) {
								try {
									replyTimesModeCursor.close();
								} catch (Throwable t) {
									Log.e(LOG_TAG,
											"Faile to close replyTimesModeCursor",
											t);
								}
							}
						}
					}
					{
						Cursor responseNumberFromCursor = null;
						try {
							responseNumberFromCursor = this.context
									.getContentResolver()
									.query(SmsReplyContract.ResponseNumberFrom.CONTENT_URI,
											null,
											SmsReplyContract.ResponseNumberFrom.REFERENCE_SMS_REPLY_ID
													+ " = ? ",
											new String[] { id.toString() },
											SmsReplyContract.ResponseNumberFrom._ID
													+ " ASC");
							List<ResponseNumberFrom> responseNumberFroms = new ArrayList<ResponseNumberFrom>();

							result.setResponseNumberFroms(responseNumberFroms);
							if (responseNumberFromCursor.moveToFirst() == true) {
								do {
									ResponseNumberFrom responseNumberFrom = null;
									int type = responseNumberFromCursor
											.getInt(responseNumberFromCursor
													.getColumnIndex(SmsReplyContract.ResponseNumberFrom.TYPE));
									if (type == ResponseNumberFrom.ALL_NUMBERS_TYPE) {
										ResponseNumberFromAllNumbers responseNumberFromAll = new ResponseNumberFromAllNumbers();
										responseNumberFrom = responseNumberFromAll;
									} else if (type == ResponseNumberFrom.ALL_NUMBERS_IN_MY_CONTACTS_TYPE) {
										ResponseNumberFromAllNumbersInMyContacts responseNumberFromAllNumbersInMyContacts = new ResponseNumberFromAllNumbersInMyContacts();
										responseNumberFrom = responseNumberFromAllNumbersInMyContacts;
									} else if (type == ResponseNumberFrom.ALL_NUMBERS_NOT_IN_MY_CONTACTS_TYPE) {
										ResponseNumberFromAllNumbersNotInMyContacts responseNumberFromAllNumbersNotInMyContacts = new ResponseNumberFromAllNumbersNotInMyContacts();
										responseNumberFrom = responseNumberFromAllNumbersNotInMyContacts;
									} else if (type == ResponseNumberFrom.CHOOSE_FROM_CONTACTS_TYPE) {
										ResponseNumberFromChooseFromMyContacts responseNumberFromContact = new ResponseNumberFromChooseFromMyContacts();

										if (responseNumberFromCursor
												.isNull(responseNumberFromCursor
														.getColumnIndex(SmsReplyContract.ResponseNumberFrom.NAME)) == false) {
											responseNumberFromContact
													.setName(responseNumberFromCursor
															.getString(responseNumberFromCursor
																	.getColumnIndex(SmsReplyContract.ResponseNumberFrom.NAME)));
										}
										responseNumberFromContact
												.setPhoneNumber(responseNumberFromCursor
														.getString(responseNumberFromCursor
																.getColumnIndex(SmsReplyContract.ResponseNumberFrom.PHONE_NUMBER)));
										responseNumberFrom = responseNumberFromContact;
									} else if (type == ResponseNumberFrom.USER_INPUT_TYPE) {
										ResponseNumberFromUserInput responseNumberFromUserInput = new ResponseNumberFromUserInput();

										if (responseNumberFromCursor
												.isNull(responseNumberFromCursor
														.getColumnIndex(SmsReplyContract.ResponseNumberFrom.NAME)) == false) {
											responseNumberFromUserInput
													.setName(responseNumberFromCursor
															.getString(responseNumberFromCursor
																	.getColumnIndex(SmsReplyContract.ResponseNumberFrom.NAME)));
										}
										responseNumberFromUserInput
												.setPhoneNumber(responseNumberFromCursor
														.getString(responseNumberFromCursor
																.getColumnIndex(SmsReplyContract.ResponseNumberFrom.PHONE_NUMBER)));
										responseNumberFrom = responseNumberFromUserInput;
									} else if (type == ResponseNumberFrom.MATCH_SMS_KEYWORD_TYPE) {
										ResponseNumberFromMatchSmsWithKeyword responseNumberFromMatchSmsWithKeyword = new ResponseNumberFromMatchSmsWithKeyword();

										responseNumberFromMatchSmsWithKeyword
												.setKeyword(responseNumberFromCursor
														.getString(responseNumberFromCursor
																.getColumnIndex(SmsReplyContract.ResponseNumberFrom.SMS_KEYWORD)));
										responseNumberFrom = responseNumberFromMatchSmsWithKeyword;
									} else if (type == ResponseNumberFrom.NUMBERS_FROM_CONTACT_GROUP_TYPE) {
										ResponseNumberFromNumbersFromContactGroup responseNumberFromNumbersFromContactGroup = new ResponseNumberFromNumbersFromContactGroup();
										if (responseNumberFromCursor
												.isNull(responseNumberFromCursor
														.getColumnIndex(SmsReplyContract.ResponseNumberFrom.GROUP_NAME)) == false) {
											responseNumberFromNumbersFromContactGroup
													.setGroupName(responseNumberFromCursor
															.getString(responseNumberFromCursor
																	.getColumnIndex(SmsReplyContract.ResponseNumberFrom.GROUP_NAME)));
										}
										if (responseNumberFromCursor
												.isNull(responseNumberFromCursor
														.getColumnIndex(SmsReplyContract.ResponseNumberFrom.NAME)) == false) {
											responseNumberFromNumbersFromContactGroup
													.setName(responseNumberFromCursor
															.getString(responseNumberFromCursor
																	.getColumnIndex(SmsReplyContract.ResponseNumberFrom.NAME)));
										}
										responseNumberFromNumbersFromContactGroup
												.setPhoneNumber(responseNumberFromCursor
														.getString(responseNumberFromCursor
																.getColumnIndex(SmsReplyContract.ResponseNumberFrom.PHONE_NUMBER)));
										responseNumberFrom = responseNumberFromNumbersFromContactGroup;
									}
									responseNumberFrom
											.setId(responseNumberFromCursor
													.getLong(responseNumberFromCursor
															.getColumnIndex(SmsReplyContract.ResponseNumberFrom._ID)));
									responseNumberFrom
											.setCreationDateTime(new Date(
													responseNumberFromCursor
															.getLong(responseNumberFromCursor
																	.getColumnIndex(SmsReplyContract.ResponseNumberFrom.CREATION_DATE_TIME))));
									responseNumberFroms.add(responseNumberFrom);
								} while (responseNumberFromCursor.moveToNext() == true);
							}
						} finally {
							if (responseNumberFromCursor != null) {
								try {
									responseNumberFromCursor.close();
								} catch (Throwable t) {
									Log.e(LOG_TAG,
											"Faile to close responseNumberFromCursor",
											t);
								}
							}
						}
					}
					{
						Cursor replyToSpecificNumberCursor = null;
						try {
							replyToSpecificNumberCursor = this.context
									.getContentResolver()
									.query(SmsReplyContract.ReplyToSpecificNumbers.CONTENT_URI,
											null,
											SmsReplyContract.ReplyToSpecificNumbers.REFERENCE_SMS_REPLY_ID
													+ " = ? ",
											new String[] { id.toString() },
											SmsReplyContract.ResponseNumberFrom._ID
													+ " ASC");
							List<ReplyToSpecificNumber> replyToSpecificNumbers = new ArrayList<ReplyToSpecificNumber>();
							result.setReplyToSpecificNumbers(replyToSpecificNumbers);
							if (replyToSpecificNumberCursor.moveToFirst() == true) {
								do {
									ReplyToSpecificNumber replyToSpecificNumber = null;
									int type = replyToSpecificNumberCursor
											.getInt(replyToSpecificNumberCursor
													.getColumnIndex(SmsReplyContract.ReplyToSpecificNumbers.TYPE));
									if (type == ReplyToSpecificNumber.FROM_CONTACT_TYPE) {
										ReplyToSpecificNumberFromContact replyToSpecificNumberFromContact = new ReplyToSpecificNumberFromContact();
										if (replyToSpecificNumberCursor
												.isNull(replyToSpecificNumberCursor
														.getColumnIndex(SmsReplyContract.ReplyToSpecificNumbers.NAME)) == false) {
											replyToSpecificNumberFromContact
													.setName(replyToSpecificNumberCursor
															.getString(replyToSpecificNumberCursor
																	.getColumnIndex(SmsReplyContract.ReplyToSpecificNumbers.NAME)));
										}
										replyToSpecificNumberFromContact
												.setPhoneNumber(replyToSpecificNumberCursor
														.getString(replyToSpecificNumberCursor
																.getColumnIndex(SmsReplyContract.ReplyToSpecificNumbers.PHONE_NUMBER)));
										replyToSpecificNumber = replyToSpecificNumberFromContact;
									} else if (type == ReplyToSpecificNumber.USER_INPUT_TYPE) {
										ReplyToSpecificNumberFromUserInput replyToSpecificNumberFromUserInput = new ReplyToSpecificNumberFromUserInput();
										if (replyToSpecificNumberCursor
												.isNull(replyToSpecificNumberCursor
														.getColumnIndex(SmsReplyContract.ReplyToSpecificNumbers.NAME)) == false) {
											replyToSpecificNumberFromUserInput
													.setName(replyToSpecificNumberCursor
															.getString(replyToSpecificNumberCursor
																	.getColumnIndex(SmsReplyContract.ReplyToSpecificNumbers.NAME)));
										}
										replyToSpecificNumberFromUserInput
												.setPhoneNumber(replyToSpecificNumberCursor
														.getString(replyToSpecificNumberCursor
																.getColumnIndex(SmsReplyContract.ReplyToSpecificNumbers.PHONE_NUMBER)));
										replyToSpecificNumber = replyToSpecificNumberFromUserInput;
									} else if (type == ReplyToSpecificNumber.NUMBERS_FROM_CONTACT_GROUP_TYPE) {
										ReplyToSpecificNumberFromNumbersFromContactGroup replyToSpecificNumberFromNumbersFromContactGroup = new ReplyToSpecificNumberFromNumbersFromContactGroup();
										if (replyToSpecificNumberCursor
												.isNull(replyToSpecificNumberCursor
														.getColumnIndex(SmsReplyContract.ReplyToSpecificNumbers.NAME)) == false) {
											replyToSpecificNumberFromNumbersFromContactGroup
													.setName(replyToSpecificNumberCursor
															.getString(replyToSpecificNumberCursor
																	.getColumnIndex(SmsReplyContract.ReplyToSpecificNumbers.NAME)));
										}
										if (replyToSpecificNumberCursor
												.isNull(replyToSpecificNumberCursor
														.getColumnIndex(SmsReplyContract.ReplyToSpecificNumbers.GROUP_NAME)) == false) {
											replyToSpecificNumberFromNumbersFromContactGroup
													.setGroupName(replyToSpecificNumberCursor
															.getString(replyToSpecificNumberCursor
																	.getColumnIndex(SmsReplyContract.ReplyToSpecificNumbers.GROUP_NAME)));
										}
										replyToSpecificNumberFromNumbersFromContactGroup
												.setPhoneNumber(replyToSpecificNumberCursor
														.getString(replyToSpecificNumberCursor
																.getColumnIndex(SmsReplyContract.ReplyToSpecificNumbers.PHONE_NUMBER)));
										replyToSpecificNumber = replyToSpecificNumberFromNumbersFromContactGroup;
									}
									replyToSpecificNumber
											.setId(replyToSpecificNumberCursor
													.getInt(replyToSpecificNumberCursor
															.getColumnIndex(SmsReplyContract.ReplyToSpecificNumbers._ID)));
									replyToSpecificNumber
											.setCreationDateTime(new Date(
													replyToSpecificNumberCursor
															.getLong(replyToSpecificNumberCursor
																	.getColumnIndex(SmsReplyContract.ReplyToSpecificNumbers.CREATION_DATE_TIME))));
									replyToSpecificNumbers
											.add(replyToSpecificNumber);
								} while (replyToSpecificNumberCursor
										.moveToNext() == true);
							}
						} finally {
							if (replyToSpecificNumberCursor != null) {
								try {
									replyToSpecificNumberCursor.close();
								} catch (Throwable t) {
									Log.e(LOG_TAG,
											"Faile to close replyToSpecificNumberCursor",
											t);
								}
							}
						}
					}
					{
						Cursor noneReplyNumberCursor = null;
						try {
							noneReplyNumberCursor = this.context
									.getContentResolver()
									.query(SmsReplyContract.NoneReplyNumbers.CONTENT_URI,
											null,
											SmsReplyContract.NoneReplyNumbers.REFERENCE_SMS_REPLY_ID
													+ " = ? ",
											new String[] { id.toString() },
											SmsReplyContract.NoneReplyNumbers._ID
													+ " ASC");
							List<NoneReplyNumber> noneReplyNumbers = new ArrayList<NoneReplyNumber>();
							result.setNoneReplyNumbers(noneReplyNumbers);
							if (noneReplyNumberCursor.moveToFirst() == true) {
								do {
									NoneReplyNumber noneReplyNumber = null;
									int type = noneReplyNumberCursor
											.getInt(noneReplyNumberCursor
													.getColumnIndex(SmsReplyContract.NoneReplyNumbers.TYPE));
									if (type == NoneReplyNumber.ALL_NUMBERS_IN_MY_CONTACTS_TYPE) {
										NoneReplyNumberFromContacts noneReplyNumberFromContacts = new NoneReplyNumberFromContacts();
										noneReplyNumber = noneReplyNumberFromContacts;
									} else if (type == NoneReplyNumber.CHOOSE_FROM_CONTACT_TYPE) {
										NoneReplyNumberFromContact noneReplyNumberFromContact = new NoneReplyNumberFromContact();
										if (noneReplyNumberCursor
												.isNull(noneReplyNumberCursor
														.getColumnIndex(SmsReplyContract.NoneReplyNumbers.NAME)) == false) {
											noneReplyNumberFromContact
													.setName(noneReplyNumberCursor
															.getString(noneReplyNumberCursor
																	.getColumnIndex(SmsReplyContract.NoneReplyNumbers.NAME)));
										}
										noneReplyNumberFromContact
												.setPhoneNumber(noneReplyNumberCursor
														.getString(noneReplyNumberCursor
																.getColumnIndex(SmsReplyContract.NoneReplyNumbers.PHONE_NUMBER)));
										noneReplyNumber = noneReplyNumberFromContact;
									} else if (type == NoneReplyNumber.USER_INPUT_TYPE) {
										NoneReplyNumberFromUserInput replyToSpecificNumberFromUserInput = new NoneReplyNumberFromUserInput();
										if (noneReplyNumberCursor
												.isNull(noneReplyNumberCursor
														.getColumnIndex(SmsReplyContract.NoneReplyNumbers.NAME)) == false) {
											replyToSpecificNumberFromUserInput
													.setName(noneReplyNumberCursor
															.getString(noneReplyNumberCursor
																	.getColumnIndex(SmsReplyContract.NoneReplyNumbers.NAME)));
										}
										replyToSpecificNumberFromUserInput
												.setPhoneNumber(noneReplyNumberCursor
														.getString(noneReplyNumberCursor
																.getColumnIndex(SmsReplyContract.NoneReplyNumbers.PHONE_NUMBER)));
										noneReplyNumber = replyToSpecificNumberFromUserInput;
									} else if (type == NoneReplyNumber.MATCH_SMS_KEYWORD_TYPE) {
										NoneReplyNumberFromMatchSmsWithKeyword noneReplyNumberFromMatchSmsWithKeyword = new NoneReplyNumberFromMatchSmsWithKeyword();
										if (noneReplyNumberCursor
												.isNull(noneReplyNumberCursor
														.getColumnIndex(SmsReplyContract.NoneReplyNumbers.SMS_KEYWORD)) == false) {
											noneReplyNumberFromMatchSmsWithKeyword
													.setKeyword(noneReplyNumberCursor
															.getString(noneReplyNumberCursor
																	.getColumnIndex(SmsReplyContract.NoneReplyNumbers.SMS_KEYWORD)));
										}
										noneReplyNumber = noneReplyNumberFromMatchSmsWithKeyword;
									} else if (type == NoneReplyNumber.NUMBERS_FROM_CONTACT_GROUP_TYPE) {
										NoneReplyNumberFromNumbersFromContactGroup noneReplyNumberFromMatchSmsWithKeyword = new NoneReplyNumberFromNumbersFromContactGroup();
										if (noneReplyNumberCursor
												.isNull(noneReplyNumberCursor
														.getColumnIndex(SmsReplyContract.NoneReplyNumbers.NAME)) == false) {
											noneReplyNumberFromMatchSmsWithKeyword
													.setName(noneReplyNumberCursor
															.getString(noneReplyNumberCursor
																	.getColumnIndex(SmsReplyContract.NoneReplyNumbers.NAME)));
										}
										if (noneReplyNumberCursor
												.isNull(noneReplyNumberCursor
														.getColumnIndex(SmsReplyContract.NoneReplyNumbers.GROUP_NAME)) == false) {
											noneReplyNumberFromMatchSmsWithKeyword
													.setGroupName(noneReplyNumberCursor
															.getString(noneReplyNumberCursor
																	.getColumnIndex(SmsReplyContract.NoneReplyNumbers.GROUP_NAME)));
										}
										noneReplyNumberFromMatchSmsWithKeyword
												.setPhoneNumber(noneReplyNumberCursor
														.getString(noneReplyNumberCursor
																.getColumnIndex(SmsReplyContract.NoneReplyNumbers.PHONE_NUMBER)));
										noneReplyNumber = noneReplyNumberFromMatchSmsWithKeyword;
									}
									noneReplyNumber
											.setId(noneReplyNumberCursor
													.getInt(noneReplyNumberCursor
															.getColumnIndex(SmsReplyContract.NoneReplyNumbers._ID)));
									noneReplyNumber
											.setCreationDateTime(new Date(
													noneReplyNumberCursor
															.getLong(noneReplyNumberCursor
																	.getColumnIndex(SmsReplyContract.NoneReplyNumbers.CREATION_DATE_TIME))));
									noneReplyNumbers.add(noneReplyNumber);
								} while (noneReplyNumberCursor.moveToNext() == true);
							}
						} finally {
							if (noneReplyNumberCursor != null) {
								try {
									noneReplyNumberCursor.close();
								} catch (Throwable t) {
									Log.e(LOG_TAG,
											"Faile to close noneReplyNumberCursor",
											t);
								}
							}
						}
					}
				}
			} finally {
				if (smsReplyCursor != null) {
					try {
						smsReplyCursor.close();
					} catch (Throwable t) {
						Log.e(LOG_TAG, "Faile to close smsReplyCursor", t);
					}
				}
			}

		} finally {
		}

		return result;
	}

	public Long getActivationRecordId(long smsReplyId) {
		Long result = null;
		Cursor activationRecordCursor = null;
		try {
			activationRecordCursor = this.context
					.getContentResolver()
					.query(SmsReplyContract.ActivationRecords.CONTENT_URI,
							new String[] { SmsReplyContract.ActivationRecords._ID },
							SmsReplyContract.ActivationRecords.IS_ACTIVATED
									+ " = ? "
									+ " AND "
									+ SmsReplyContract.ActivationRecords.REFERENCE_SMS_REPLY_ID
									+ " = ? ",
							new String[] { String.valueOf(1),
									String.valueOf(smsReplyId) }, null);
			if (activationRecordCursor.moveToFirst() == true) {
				if (activationRecordCursor
						.isNull(activationRecordCursor
								.getColumnIndexOrThrow(SmsReplyContract.ActivationRecords._ID)) == false) {
					result = activationRecordCursor
							.getLong(activationRecordCursor
									.getColumnIndexOrThrow(SmsReplyContract.ActivationRecords._ID));
				}
			}
		} catch (Throwable throwable) {
			Log.e(LOG_TAG, "Unknown Error!", throwable);
		} finally {
			if (activationRecordCursor != null) {
				try {
					activationRecordCursor.close();
				} catch (Throwable th) {
					Log.e(LOG_TAG, "Fail to closeactivationRecordCursor", th);
				}
			}
		}
		return result;
	}

	public synchronized boolean switchSmsReplyActivateStatus(Long id)
			throws NoPhoneMessageException, NoReplyMessageException,
			NoSmsMessageException, ReplyDateTimeInvalidException,
			ReplyDateTimeExpiredException, SmsReplyNotExistException,
			RemoteException, OperationApplicationException {
		boolean result = false;
		SmsReply smsReply = this.read(id);
		if (smsReply == null) {
			String message = "SmsReply has been deleted. Id: " + id;
			throw new SmsReplyNotExistException(message);
		}

		ArrayList<ContentProviderOperation> contentProviderOperations = new ArrayList<ContentProviderOperation>();
		if (smsReply.getIsActivated() == false) {
			try {
				boolean shouldActivated = false;
				if (smsReply.getMissedCallAndSmsUsedDifferentMessages() == false) {
					if (TextUtils.isEmpty(smsReply.getPhoneMessage()) == true) {
						String message = "No message. Id: " + id;
						throw new NoReplyMessageException(message);
					}
				} else {
					if (TextUtils.isEmpty(smsReply.getPhoneMessage()) == true) {
						String message = "No message. Id: " + id;
						throw new NoPhoneMessageException(message);
					}
					if (TextUtils.isEmpty(smsReply.getSmsMessage()) == true) {
						String message = "No message. Id: " + id;
						throw new NoSmsMessageException(message);
					}
				}

				if (smsReply.getReplyDateTimeModeType() == SmsReply.REPLY_DATE_TIME_MODE_ALWAYS_TYPE) {
					shouldActivated = true;
				} else if (smsReply.getReplyDateTimeModeType() == SmsReply.REPLY_DATE_TIME_MODE_EXPIRED_AFTER_TYPE) {
					if (smsReply.getExpiredAfterMilliSeconds() == 0l) {
						String message = "Expired After Error! Could not expired after 0!";
						throw new ReplyDateTimeInvalidException(message);
					}
					shouldActivated = true;
				} else if (smsReply.getReplyDateTimeModeType() == SmsReply.REPLY_DATE_TIME_MODE_PERIOD_TYPE) {
					Date currentDate = new Date();
					Date startDate = smsReply.getPeriodStartDateTime();
					Date endDate = smsReply.getPeriodEndDateTime();
					if (startDate.getTime() >= endDate.getTime()) {
						String message = "End time is earlier than start time.";
						throw new ReplyDateTimeExpiredException(message);
					} else {
						shouldActivated = true;
					}

					if ((currentDate.getTime() < endDate.getTime()) == false) {
						String message = "Has expired";
						throw new ReplyDateTimeExpiredException(message);
					} else {
						shouldActivated = true;
					}
				} else if (smsReply.getReplyDateTimeModeType() == SmsReply.REPLY_DATE_TIME_MODE_REPEAT_TYPE) {
					Calendar currentCalendar = Calendar.getInstance();
					Date startDate = smsReply.getRepeatStartTime();
					Date endDate = smsReply.getRepeatEndTime();
					Calendar startCalendar = Calendar.getInstance();
					Calendar endCalendar = Calendar.getInstance();
					startCalendar.setTime(startDate);
					endCalendar.setTime(endDate);

					int currentYear = currentCalendar.get(Calendar.YEAR);
					int currentMonth = currentCalendar.get(Calendar.MONTH);
					int currentDay = currentCalendar.get(Calendar.DAY_OF_YEAR);

					startCalendar.set(Calendar.YEAR, currentYear);
					startCalendar.set(Calendar.MONTH, currentMonth);
					startCalendar.set(Calendar.DAY_OF_YEAR, currentDay);
					startCalendar.set(Calendar.MILLISECOND, 0);
					endCalendar.set(Calendar.YEAR, currentYear);
					endCalendar.set(Calendar.MONTH, currentMonth);
					endCalendar.set(Calendar.DAY_OF_YEAR, currentDay);
					endCalendar.set(Calendar.MILLISECOND, 999);

					if (DateTimeUtil.isNeverRepeat(smsReply) == true) {
						if (currentCalendar.getTimeInMillis() > startCalendar
								.getTimeInMillis()) {
							startCalendar.add(Calendar.DAY_OF_YEAR, 1);
							endCalendar.add(Calendar.DAY_OF_YEAR, 1);
						}

						if (endCalendar.getTimeInMillis() <= startCalendar
								.getTimeInMillis()) {
							endCalendar.add(Calendar.DAY_OF_YEAR, 1);
						}

						if (currentCalendar.getTimeInMillis() < endCalendar
								.getTimeInMillis()) {
							ContentValues updateSmsReplyContentValues = new ContentValues();
							updateSmsReplyContentValues
									.put(SmsReplyContract.SmsReplies.REPEAT_START_TIME,
											startCalendar.getTimeInMillis());
							updateSmsReplyContentValues
									.put(SmsReplyContract.SmsReplies.REPEAT_END_TIME,
											endCalendar.getTimeInMillis());
							updateSmsReplyContentValues
									.put(SmsReplyContract.SmsReplies.REPLY_DATE_TIME_TYPE,
											SmsReplyContract.SmsReplies.REPLY_DATE_TIME_MODE_REPEAT_TYPE);
							updateSmsReplyContentValues
									.put(SmsReplyContract.SmsReplies.REPEAT_START_TIME,
											startCalendar.getTimeInMillis());
							updateSmsReplyContentValues
									.put(SmsReplyContract.SmsReplies.REPEAT_END_TIME,
											endCalendar.getTimeInMillis());

							this.context.getContentResolver().update(
									SmsReplyContract.SmsReplies.CONTENT_URI,
									updateSmsReplyContentValues,
									SmsReplyContract.SmsReplies._ID + " = ?",
									new String[] { id.toString() });
							shouldActivated = true;
						} else {
							String message = "Should not happen!";
							throw new ReplyDateTimeInvalidException(message);
						}
					} else {
						shouldActivated = true;
					}

				}
				if (shouldActivated == true) {
					{
						ContentProviderOperation.Builder updateActivationRecordsBuilder = ContentProviderOperation
								.newUpdate(SmsReplyContract.ActivationRecords.CONTENT_URI);

						ContentValues updateActivaionRecord = new ContentValues();
						updateActivaionRecord
								.put(SmsReplyContract.ActivationRecords.IS_ACTIVATED,
										FALSE_VALUE.intValue());
						updateActivationRecordsBuilder
								.withSelection(
										SmsReplyContract.ActivationRecords.REFERENCE_SMS_REPLY_ID
												+ " = ? ",
										new String[] { id.toString() })
								.withValues(updateActivaionRecord);
						contentProviderOperations
								.add(updateActivationRecordsBuilder.build());

						ContentProviderOperation.Builder insertActivationRecordsBuilder = ContentProviderOperation
								.newInsert(SmsReplyContract.ActivationRecords.CONTENT_URI);

						ContentValues insertActivationRecordContentValues = new ContentValues();
						insertActivationRecordContentValues
								.put(SmsReplyContract.ActivationRecords.CREATION_DATE_TIME,
										new Date().getTime());
						insertActivationRecordContentValues
								.put(SmsReplyContract.ActivationRecords.IS_ACTIVATED,
										TRUE_VALUE.intValue());
						insertActivationRecordContentValues
								.put(SmsReplyContract.ActivationRecords.REFERENCE_SMS_REPLY_ID,
										id);
						contentProviderOperations
								.add(insertActivationRecordsBuilder.withValues(
										insertActivationRecordContentValues)
										.build());
					}
					{
						Date currentDate = new Date();
						ContentValues smsReplyContentValues = new ContentValues();
						smsReplyContentValues.put(
								SmsReplyContract.SmsReplies.IS_ACTIVATED,
								TRUE_VALUE);
						smsReplyContentValues
								.put(SmsReplyContract.SmsReplies.MODIFICATION_DATE_TIME,
										currentDate.getTime());
						smsReplyContentValues
								.put(SmsReplyContract.SmsReplies.ACTIVATION_DATE_TIME,
										currentDate.getTime());
						contentProviderOperations
								.add(ContentProviderOperation
										.newUpdate(
												SmsReplyContract.SmsReplies.CONTENT_URI)
										.withSelection(
												SmsReplyContract.SmsReplies._ID
														+ " = ? ",
												new String[] { id.toString() })
										.withValues(smsReplyContentValues)
										.build());
						this.context.getContentResolver().update(
								SmsReplyContract.SmsReplies.CONTENT_URI,
								smsReplyContentValues,
								SmsReplyContract.SmsReplies._ID + " = ? ",
								new String[] { id.toString() });

						this.context.getContentResolver().applyBatch(
								SmsReplyContract.AUTHORITY,
								contentProviderOperations);
						Log.i(LOG_TAG,
								"Successful turn on SmsReply which id is: "
										+ id);

						result = true;
					}
				}

			} catch (NoPhoneMessageException noPhoneMessageException) {
				Log.e(LOG_TAG, "No phone message", noPhoneMessageException);
				throw noPhoneMessageException;
			} catch (NoReplyMessageException noReplyMessageException) {
				Log.e(LOG_TAG, "No reply message", noReplyMessageException);
				throw noReplyMessageException;
			} catch (NoSmsMessageException noSmsMessageException) {
				Log.e(LOG_TAG, "No SMS message", noSmsMessageException);
				throw noSmsMessageException;
			} catch (ReplyDateTimeInvalidException replyDateTimeInvalidException) {
				Log.e(LOG_TAG, "Reply Date Time is invalid!",
						replyDateTimeInvalidException);
				throw replyDateTimeInvalidException;
			} catch (ReplyDateTimeExpiredException replyDateTimeExpiredException) {
				Log.e(LOG_TAG, "Reply Date Time is expired!",
						replyDateTimeExpiredException);
				throw replyDateTimeExpiredException;
			}
		} else {
			try {
				ContentValues activatioinRecordContentValues = new ContentValues();
				activatioinRecordContentValues.put(
						SmsReplyContract.ActivationRecords.IS_ACTIVATED,
						FALSE_VALUE.toString());
				contentProviderOperations
						.add(ContentProviderOperation
								.newUpdate(
										SmsReplyContract.ActivationRecords.CONTENT_URI)
								.withValues(activatioinRecordContentValues)
								.withSelection(
										SmsReplyContract.ActivationRecords.IS_ACTIVATED
												+ " = ? "
												+ " AND "
												+ SmsReplyContract.ActivationRecords.REFERENCE_SMS_REPLY_ID
												+ " = ? ",
										new String[] { TRUE_VALUE.toString(),
												id.toString() }).build());
				Date currentDate = new Date();
				ContentValues smsReplyContentValues = new ContentValues();
				smsReplyContentValues.put(
						SmsReplyContract.SmsReplies.IS_ACTIVATED, FALSE_VALUE);
				smsReplyContentValues.put(
						SmsReplyContract.SmsReplies.MODIFICATION_DATE_TIME,
						currentDate.getTime());
				contentProviderOperations.add(ContentProviderOperation
						.newUpdate(SmsReplyContract.SmsReplies.CONTENT_URI)
						.withValues(smsReplyContentValues)
						.withSelection(
								SmsReplyContract.SmsReplies._ID + " = ?",
								new String[] { id.toString() }).build());

				this.context.getContentResolver().applyBatch(
						SmsReplyContract.AUTHORITY, contentProviderOperations);
				Log.i(LOG_TAG, "Successful turn off SmsReply which id is: "
						+ id);
			} catch (Throwable throwable) {
				Log.e(LOG_TAG, "Update SmsReply: " + id
						+ " to inactivated error!", throwable);
			}

			result = false;
		}
		List<Integer> appwidgetIds = new ArrayList<Integer>();
		Cursor cursor = null;
		try {
			cursor = this.context
					.getContentResolver()
					.query(SmsReplyContract.SmsReplyAppWidgetIds.CONTENT_URI,
							new String[] { SmsReplyContract.SmsReplyAppWidgetIds.APP_WIDGET_ID },
							SmsReplyContract.SmsReplyAppWidgetIds.REFERENCE_SMS_REPLY_ID
									+ " = ?",
							new String[] { String.valueOf(id) }, null);
			if (cursor.moveToFirst() == true) {
				do {
					appwidgetIds
							.add(cursor.getInt(cursor
									.getColumnIndex(SmsReplyContract.SmsReplyAppWidgetIds.APP_WIDGET_ID)));
				} while (cursor.moveToNext() == true);
			}
		} catch (Throwable throwable) {
			Log.e(LOG_TAG, "Update SmsReply: " + id + " to inactivated error!",
					throwable);
		} finally {
			if (cursor != null) {
				try {
					cursor.close();
				} catch (Throwable th) {
					Log.e(LOG_TAG, "Unable to close sqlite.", th);
				}
			}
		}
		AppWidgetManager appWidgetManager = AppWidgetManager
				.getInstance(context);
		for (Integer appId : appwidgetIds) {
			SmsReplyAppWidgetProvider.updateAppWidget(context,
					appWidgetManager, id, appId, result, smsReply.getTitle());
		}
		this.context.getContentResolver().notifyChange(
				SmsReplyContract.SmsReplies.CONTENT_URI, null);

		Intent scheduleNotifyNotificationServiceIntent = new Intent(context,
				SmsReplyScheduleNotifyNotificationInentService.class);
		Bundle argumentsForSchedule = new Bundle();
		argumentsForSchedule
				.putInt(SmsReplyScheduleNotifyNotificationInentService.ARGUMENTS_ACTION_TYPE,
						SmsReplyScheduleNotifyNotificationInentService.ACTION_TYPE_SCHEDULE_SPECIFIC_SMS_REPLY);
		argumentsForSchedule
				.putLong(
						SmsReplyScheduleNotifyNotificationInentService.ARGUMENTS_SMS_REPLY_ID,
						id);
		scheduleNotifyNotificationServiceIntent.putExtras(argumentsForSchedule);
		context.startService(scheduleNotifyNotificationServiceIntent);
		Log.d(LOG_TAG, "Start SmsReplyScheduleNotifyNotificationInentService!");

		return result;
	}

	public void turnOffSmsReply(long smsReplyId) {
		PhoneUtilitySQLiteOpenHelper phoneUtilitySQLiteOpenHelper = null;
		SQLiteDatabase sqLiteDatabase = null;
		Cursor cursor = null;
		try {
			phoneUtilitySQLiteOpenHelper = new PhoneUtilitySQLiteOpenHelper(
					this.context);
			sqLiteDatabase = phoneUtilitySQLiteOpenHelper.getWritableDatabase();
			sqLiteDatabase.beginTransaction();
			cursor = sqLiteDatabase.query(
					PhoneUtilitySQLiteOpenHelper.Tables.SMS.Reply.REPLIES,
					null, SmsReplyContract.SmsReplies._ID + " = ? ",
					new String[] { String.valueOf(smsReplyId) }, null, null,
					null);
			if (cursor.moveToFirst() == true) {
				boolean isActivated = cursor
						.getInt(cursor
								.getColumnIndex(SmsReplyContract.SmsReplies.IS_ACTIVATED)) == 1 ? true
						: false;
				if (isActivated == true) {
					long currentTimeMillis = System.currentTimeMillis();
					boolean shouldTurnItOff = false;
					int replyDateTimeMode = cursor
							.getInt(cursor
									.getColumnIndex(SmsReplyContract.SmsReplies.REPLY_DATE_TIME_TYPE));
					if (replyDateTimeMode == SmsReplyContract.SmsReplies.REPLY_DATE_TIME_MODE_ALWAYS_TYPE) {
					} else if (replyDateTimeMode == SmsReplyContract.SmsReplies.REPLY_DATE_TIME_MODE_EXPIRED_AFTER_TYPE) {
						long expiredAfter = cursor
								.getLong(cursor
										.getColumnIndex(SmsReplyContract.SmsReplies.ACTIVATION_DATE_TIME))
								+ cursor.getLong(cursor
										.getColumnIndex(SmsReplyContract.SmsReplies.EXPIRATED_AFTER_MILLISECONDS));

						if (currentTimeMillis >= expiredAfter) {
							shouldTurnItOff = true;
						}
					} else if (replyDateTimeMode == SmsReplyContract.SmsReplies.REPLY_DATE_TIME_MODE_PERIOD_TYPE) {
						long periodEndDateTimeInMillis = cursor
								.getLong(cursor
										.getColumnIndex(SmsReplyContract.SmsReplies.PERIOD_END_DATE_TIME));
						if (currentTimeMillis >= periodEndDateTimeInMillis) {
							shouldTurnItOff = true;
						}
					} else if (replyDateTimeMode == SmsReplyContract.SmsReplies.REPLY_DATE_TIME_MODE_REPEAT_TYPE) {
						if (cursor
								.getInt(cursor
										.getColumnIndex(SmsReplyContract.SmsReplies.IS_SUNDAY_ENABLE)) == 0
								&& cursor
										.getInt(cursor
												.getColumnIndex(SmsReplyContract.SmsReplies.IS_MONDAY_ENABLE)) == 0
								&& cursor
										.getInt(cursor
												.getColumnIndex(SmsReplyContract.SmsReplies.IS_TUESDAY_ENABLE)) == 0
								&& cursor
										.getInt(cursor
												.getColumnIndex(SmsReplyContract.SmsReplies.IS_WEDNESDAY_ENABLE)) == 0
								&& cursor
										.getInt(cursor
												.getColumnIndex(SmsReplyContract.SmsReplies.IS_THURSDAY_ENABLE)) == 0
								&& cursor
										.getInt(cursor
												.getColumnIndex(SmsReplyContract.SmsReplies.IS_FRIDAY_ENABLE)) == 0
								&& cursor
										.getInt(cursor
												.getColumnIndex(SmsReplyContract.SmsReplies.IS_SATURDAY_ENABLE)) == 0) {
							long repeatEndTime = cursor
									.getLong(cursor
											.getColumnIndex(SmsReplyContract.SmsReplies.REPEAT_END_TIME));
							if (currentTimeMillis >= repeatEndTime) {
								shouldTurnItOff = true;
							}
						} else {
							Log.d(LOG_TAG, "Repeat in some day!");
						}
					} else {
						Log.e(LOG_TAG,
								"replyDateTimeModeType does not match one! replyDateTimeModeType: "
										+ replyDateTimeMode);
					}
					if (shouldTurnItOff == true) {
						ContentValues updateSmsReplyContentValues = new ContentValues();
						updateSmsReplyContentValues.put(
								SmsReplyContract.SmsReplies.IS_ACTIVATED, 0);
						updateSmsReplyContentValues
								.put(SmsReplyContract.SmsReplies.MODIFICATION_DATE_TIME,
										currentTimeMillis);
						int updateCount = sqLiteDatabase
								.update(PhoneUtilitySQLiteOpenHelper.Tables.SMS.Reply.REPLIES,
										updateSmsReplyContentValues,
										SmsReplyContract.SmsReplies._ID
												+ " = ? ",
										new String[] { String
												.valueOf(smsReplyId) });

						this.context.getContentResolver().notifyChange(
								SmsReplyContract.SmsReplies.CONTENT_URI, null);
						if (updateCount > 0) {
							Log.d(LOG_TAG, "Update Sms Reply Id: " + smsReplyId
									+ " to non-activated");

							Intent updateNotificationServiceIntent = new Intent(
									this.context,
									SmsReplyNotificationIntentService.class);
							Bundle arguments = new Bundle();
							arguments
									.putInt(SmsReplyNotificationIntentService.ARGUMENTS_ACTION_TYPE,
											SmsReplyNotificationIntentService.ACTION_TYPE_SMS_REPLY_SINGLE);
							arguments
									.putLong(
											SmsReplyNotificationIntentService.ARGUMENTS_SMS_REPLY_ID,
											smsReplyId);
							updateNotificationServiceIntent
									.putExtras(arguments);
							this.context
									.startService(updateNotificationServiceIntent);
							Log.d(LOG_TAG,
									"Start SmsReplyNotificationIntentService!");
						} else {
							Log.d(LOG_TAG, "Sms Reply has been deleted. Id: "
									+ smsReplyId);
						}
					} else {
						Log.d(LOG_TAG, "Should not Turn Sms Reply Id: "
								+ smsReplyId + " to off!");
					}
				} else {
					Intent updateNotificationServiceIntent = new Intent(
							this.context,
							SmsReplyNotificationIntentService.class);
					Bundle arguments = new Bundle();
					arguments
							.putInt(SmsReplyNotificationIntentService.ARGUMENTS_ACTION_TYPE,
									SmsReplyNotificationIntentService.ACTION_TYPE_SMS_REPLY_RECHECK);
					updateNotificationServiceIntent.putExtras(arguments);
					this.context.startService(updateNotificationServiceIntent);
					Log.d(LOG_TAG, "Sms Reply has been deleted. Id: "
							+ smsReplyId);
				}
			} else {
				Intent updateNotificationServiceIntent = new Intent(
						this.context, SmsReplyNotificationIntentService.class);
				Bundle arguments = new Bundle();
				arguments
						.putInt(SmsReplyNotificationIntentService.ARGUMENTS_ACTION_TYPE,
								SmsReplyNotificationIntentService.ACTION_TYPE_SMS_REPLY_SINGLE);
				arguments
						.putLong(
								SmsReplyNotificationIntentService.ARGUMENTS_SMS_REPLY_ID,
								smsReplyId);
				updateNotificationServiceIntent.putExtras(arguments);
				this.context.startService(updateNotificationServiceIntent);
				Log.i(LOG_TAG, "Sms Reply is inactivated. Id: " + smsReplyId);
			}

			sqLiteDatabase.setTransactionSuccessful();
		} catch (Throwable throwable) {

		} finally {
			sqLiteDatabase.endTransaction();
			if (cursor != null && cursor.isClosed() == false) {
				try {
					cursor.close();
				} catch (Throwable th) {
					Log.e(LOG_TAG, "Fail to close cursor", th);
				}
			}
			if (sqLiteDatabase != null) {
				try {
					sqLiteDatabase.close();
				} catch (Throwable th) {
					Log.e(LOG_TAG, "Fail to close sqLiteDatabase", th);
				}
			}
			if (phoneUtilitySQLiteOpenHelper != null) {
				try {
					phoneUtilitySQLiteOpenHelper.close();
				} catch (Throwable th) {
					Log.e(LOG_TAG,
							"Fail to close phoneUtilitySQLiteOpenHelper", th);
				}
			}

		}

	}

	public boolean isActivated(Integer id) {
		boolean result = false;

		Cursor smsReplyCursor = null;
		try {
			smsReplyCursor = this.context.getContentResolver().query(
					SmsReplyContract.SmsReplies.CONTENT_URI,
					new String[] { SmsReplyContract.SmsReplies.IS_ACTIVATED },
					SmsReplyContract.SmsReplies._ID + " = ? ",
					new String[] { id.toString() }, null);
			if (smsReplyCursor.moveToFirst() == true) {
				result = (1 == smsReplyCursor
						.getInt(smsReplyCursor
								.getColumnIndex(SmsReplyContract.SmsReplies.IS_ACTIVATED)) ? true
						: false);
			}
		} finally {
			if (smsReplyCursor != null) {
				try {
					smsReplyCursor.close();
				} catch (Throwable t) {
					Log.e(LOG_TAG, "Fail to close smsReplyCursor", t);

				}
			}
		}

		return result;
	}

	private String generatePlaceHolder(int length) {
		if (length < 1) {
			throw new RuntimeException("Do not need any placeholders.");
		} else {
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append("?");
			for (int i = 1; i < length; i++) {
				stringBuilder.append(",?");
			}
			return stringBuilder.toString();
		}
	}
}
